import {
    $wuxRater
} from "./components/wux";
import {
    getWeixinUsers
} from './config.js'
import util from './utils/util.js'
import {
    request
} from './utils/request.js'

App({
    onLaunch: function() {
        let self = this;

        wx.getSystemInfo({
            success: function(res) {
                const {
                    windowHeight: screenHeight,
                    windowWidth: screenWidth
                } = res;
                self.globalData.screen = {
                    screenHeight,
                    screenWidth
                };
            }
        })

        //调用登录接口，获取 code  
        wx.login({
            success: function(res) {
                console.log(res);
                request({
                    url: getWeixinUsers,
                    method: 'POST',
                    data: {
                        appId: "wx93aa0fae8aa08037",
                        appType: "mini",
                        code: res.code
                    }
                }).then(data => {
                    wx.setStorageSync("openid", data.obj.openid);
                    self.globalData.openId = data.obj.openid
                    self.globalData.session_key = data.obj.session_key


                    if (data === null) {
                        wx.showModal({
                            title: '用户信息未授权',
                            content: '如需正常使用找公厕功能，请按确定并在授权管理中选中“用户信息”，然后点按确定。最后再重新进入小程序即可正常使用。',
                            showCancel: false,
                            success: function(res) {
                                if (res.confirm) {
                                    console.log('用户点击确定')
                                    wx.openSetting({
                                        success: function success(res) {
                                            console.log('openSetting success', res.authSetting);
                                        }
                                    });
                                }
                            }
                        })
                    }
                })
            }
        })

        // 获取用户信息
        wx.getSetting({
            success: res => {
                console.log("getSetting success", res)

                if (res.authSetting['scope.userLocation']) {
                    wx.getLocation({
                        type: 'gcj02',
                        success: function(res) {
                            self.globalData.latitude = res.latitude;
                            self.globalData.longitude = res.longitude;
                        }
                    })
                } else if (res.authSetting['scope.userLocation'] === false) {
                    wx.showModal({
                        title: '用户未授权位置权限',
                        content: '如需正常使用找公厕功能，请按确定并在授权管理中选中“用户信息”，然后点按确定。最后再重新进入小程序即可正常使用。',
                        showCancel: false,
                        success: function(res) {
                            if (res.confirm) {
                                console.log('用户点击确定')
                                wx.openSetting({
                                    success: function success(res) {
                                        console.log('openSetting success', res.authSetting);
                                    }
                                });
                            }
                        }
                    })
                }
            }
        })
    },

    globalData: {
        userInfo: {
            headimgurl: '',
            nickName: ''
        },
        openId: null,
        session_key: null,
        longitude: null,
        latitude: null,
        screen: {},
        code: null,
        token: null
    }
})